package cn.knight.user.controller;

import cn.knight.basic.annotation.PreAuthorize;
import cn.knight.basic.exception.BusinessException;
import cn.knight.basic.utils.AjaxResult;
import cn.knight.basic.utils.PageList;
import cn.knight.basic.vo.EmailRegisterVo;
import cn.knight.basic.vo.RegisterVo;
import cn.knight.user.domain.User;
import cn.knight.user.query.UserQuery;
import cn.knight.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private  IUserService userService;


    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "用户信息新增/修改", sn = "user:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.save(user);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "用户信息删除", sn = "user:remove")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public User loadById(@PathVariable("id")Long id)
    {
        return userService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "用户信息查询", sn = "user:loadAll")
    public List<User> loadAll(){

        return userService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "用户信息列表", sn = "user:queryRows")
    public PageList<User> queryRows(@RequestBody UserQuery query) {
        return userService.queryRows(query);
    }

    /**
    * @Title: registerPhone
    * @Description:  用户通过手机号注册功能
    * @Author: winnie
    * @Version: 1.0
    * @Date:  6/7/2022 下午3:23
    * @Parameters: [registerVo]
    * @Return cn.knight.basic.utils.AjaxResult
    */
    @PostMapping("/register/phone")
    public AjaxResult registerPhone(@RequestBody RegisterVo registerVo) {
        try {
            userService.registerPhone(registerVo);
            return AjaxResult.me();
        } catch (RuntimeException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @GetMapping("/loginOut")
    public AjaxResult loginOut(HttpServletRequest request){
        try {
            userService.loginOut(request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }

    }


    /**
     * @Title: email
     * @Description: 根据邮箱注册
     * @Author: winnie
     * @Version: 1.0
     * @Date:  9/7/2022 上午1:50
     * @Parameters: [emailRegisterDto]
     * @Return cn.knight.basic.utils.AjaxResult
     */
    @PostMapping("/register/email")
    public AjaxResult email(@RequestBody EmailRegisterVo emailRegisterVo){
        try {
            userService.email(emailRegisterVo);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误,请骚后重试").setSuccess(false);
        }

    }



}
